package br.com.softwareevaluation.bo;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import br.com.softwareevaluation.dao.ConexaoUtil;
import br.com.softwareevaluation.dao.SoftwareDAO;
import br.com.softwareevaluation.domain.Software;
import br.com.softwareevaluation.exceptions.BOException;
import br.com.softwareevaluation.exceptions.DAOException;

public class SoftwareBO {
	
	public void salvar(Software software) throws BOException, DAOException {
		validar(software);
		
		Connection connection = ConexaoUtil.createConnection();
		SoftwareDAO dao = new SoftwareDAO(connection);
		
		software.setDataAtualizacao(new Date());
		
		dao.inserir(software);
		dao.closeConnection();
	}
	
	public void alterar(Software software) throws BOException, DAOException {
		validar(software);
		
		Connection connection = ConexaoUtil.createConnection();
		SoftwareDAO dao = new SoftwareDAO(connection);
		
		dao.alterar(software);
		dao.closeConnection();
	}
	
	public List<Software> buscarTodos() throws BOException, DAOException {
		Connection connection = ConexaoUtil.createConnection();
		SoftwareDAO dao = new SoftwareDAO(connection);
		
		List<Software> resultado = dao.buscarTodos();
		dao.closeConnection();
		
		return resultado;
	}

	private void validar(Software software) throws BOException {
		if(software.getNome() == null || "".equals(software.getNome())) {
			throw new BOException("Informe um nome para o software.");
		}
	}
}